/*******************************************************************************
* FileName:         SeqList.cpp
* Author:           XXX
* Student Number:   3023244XXX
* Date:             2024/09/23 11:24:55
* Version:          v1.0
* Description:      Data Structure Experiment #1
*                   作业在这个文件中完成，需要补全SeqList类的实现�?
*                   给出了构造函数与析构函数的例�?.
*                   写明必要的注�?
*******************************************************************************/

#include "SeqList.h"
#include <iostream>

/*
    使用初始化列表来初始化const变量LIST_SIZE�?
    并动态分配LIST_SIZE长度的数组�?
*/
SeqList::SeqList(const int& listSize) :LIST_SIZE(listSize)
{
    seq = new int[LIST_SIZE];
    // equal to:  seq = (int*) malloc(sizeof(int) * LIST_SIZE);
    len = 0;
}

SeqList::~SeqList(){
    delete[] seq;
}

bool SeqList::isIn(const int& data){
   // len=length();
    for(int i=0;i<len;i++){
       // std::cout<<seq[i]<<" ";
        if(data==seq[i]) return true;
    }
    return false;
}

int SeqList::length(){
    return len;
}

int SeqList::getData(const int& location){
    //len=length();
   // std::cout<<seq[1]<<"???"<<'\n';
   // for(int i=0;i<len;i++)std::cout<<seq[i]<<'\n';
    if(location>=0&&location<len)
        return seq[location];
    else
        return -1;
}

bool SeqList::insert(const int& data, const int& location){
    int size_=len;
    //std::cout<<len<<"?"<<'\n';
    if(size_==LIST_SIZE) return false;
    if(isIn(data)) return true;
    if(location>=LIST_SIZE||location<0) return false;
    if(location>=size_){
       // std::cout<<"||||"<<'\n';
        seq[location]=data;
        len++;
    }
    else{
        for(int i=size_-1;i>=0;i--){
            if(i>=location)seq[i+1]=seq[i];
            else if(i==location-1)seq[i+1]=data;
            else break;
        }
        len++;
    }
    //std::cout<<"?"<<len<<'\n';
    return true;
}
//咱说好了：测试时会保证location 小于等于元素的个数~~

bool SeqList::remove(const int& location){
   // len=length();
    int size_=len;
    if(location>=size_||location<0) return false;
    if(getData(location)==-1)return false;
    for(int i=0;i<=size_-1;i++){
        if(i<location)continue;
        else if(i>=location&&i<=size_-1)seq[i]=seq[i+1];
        //else seq[i]=0;
    }
    len--;
   /// std::cout<<"?"<<len<<'\n';
    return true;
}
